#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project：kuberCloudTest 
@File：vpcnetwork.py
@Author：admin
@Date：2024/4/10 10:28 
"""
from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getDetail, _getOperationType
from kuberCloudTest.exception import KuberCloudException
from kuberCloudTest.networks.urls import networkUrls


class vpcNetworkUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return vpcNetworkUtils(accessToken=accessToken)


def getVpcNetwork(accessToken, vpcNetworkId=None, **kwargs):
    """
    获取vpc网络
    :param accessToken:
    :param vpcNetworkId:
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   searchItem: 搜索项：0: 网络名称; 1: UUID
    :return:
    """
    content = get_utils(accessToken=accessToken).cloud_http_request.get(networkUrls.getVpcNetwork.value,
                                                                        {
                                                                            'offset': kwargs.get('offset', OFFSET),
                                                                            'limit': kwargs.get('limit', LIMIT),
                                                                            'keyword': kwargs.get('keyword', ''),
                                                                            'searchitem': kwargs.get('searchItem', 0),
                                                                        })
    if vpcNetworkId:
        total = content.get('total', 0)
        content = get_utils(accessToken=accessToken).cloud_http_request.get(
            networkUrls.getVpcNetwork.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, vpcNetworkId, 'nid')
    return content


def createVpcNetwork(accessToken):
    """
    创建vpc网络
    :param accessToken:
    :return:
    """
    pass


def removeVpcNetwork(accessToken, vpcNetworkIds):
    """
    删除vpc网络
    :param accessToken:
    :param vpcNetworkIds: VPC网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 8403
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['ids'] = vpcNetworkIds
    return get_utils(accessToken=accessToken).cloud_http_request.post(networkUrls.removeVpcNetwork.value, data=data)


def updateVpcNetwork(accessToken, vpcNetworkId, **kwargs):
    """
    更新vpc网络
    :param accessToken:
    :param vpcNetworkId:
    :param kwargs: name: VPC网络名称
                   remark: 描述
    :return:
    """
    vpcNetwork = getVpcNetwork(accessToken=accessToken, vpcNetworkId=vpcNetworkId).get('obj', None)
    if not vpcNetwork:
        raise KuberCloudException('params error', 'vpcNetwork [{}] not exist'.format(vpcNetworkId))
    operationType = 8402
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['nid'] = vpcNetworkId
    data['pnname'] = kwargs.get('name', vpcNetwork['pnname'])
    data['remark'] = kwargs.get('remark', vpcNetwork['remark'])
    return get_utils(accessToken=accessToken).cloud_http_request.post(networkUrls.updateVpcNetwork.value, data=data)


def globalShareVpcNetwork(accessToken, vpcNetworkIds):
    """
    全局共享vpc网络
    :param accessToken:
    :param vpcNetworkIds: VPC网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 8408
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['ids'] = vpcNetworkIds

    return get_utils(accessToken=accessToken).cloud_http_request.post(
        networkUrls.globalShareVpcNetwork.value, data=data)


def globalRecallVpcNetwork(accessToken, vpcNetworkIds):
    """
    全局取消共享vpc网络
    :param accessToken:
    :param vpcNetworkIds: VPC网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 8409
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['ids'] = vpcNetworkIds

    return get_utils(accessToken=accessToken).cloud_http_request.post(
        networkUrls.globalRecallVpcNetwork.value, data=data)


def updateVpcNetworkMtu(accessToken, vpcNetworkId, mtu):
    """
    更新vpc网络MTU
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param mtu: MTU值
    :return:
    """
    operationType = 8410
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['nid'] = vpcNetworkId
    data['mtu'] = mtu

    return get_utils(accessToken=accessToken).cloud_http_request.post(networkUrls.updateVpcNetworkMtu.value, data=data)


def getVpcRouterByVpcNetwork(accessToken, vpcNetworkId, **kwargs):
    """
    获取vpc网络下可挂载路由器
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   searchItem: 搜索项：0: 路由器名称; 2: UUID
    :return:
    """
    return get_utils(accessToken=accessToken).cloud_http_request.get(networkUrls.getVpcRouterByVpcNetwork.value,
                                                                     {
                                                                         'ltnid': vpcNetworkId,
                                                                         'offset': kwargs.get('offset', OFFSET),
                                                                         'limit': kwargs.get('limit', LIMIT),
                                                                         'keyword': kwargs.get('keyword', ''),
                                                                     })


def loadVpcRouterByVpcNetwork(accessToken, vpcNetworkId, routerId):
    """
    VPC网络挂载路由器
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param routerId: 路由器UUID
    :return:
    """
    operationType = 8406
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['nid'] = vpcNetworkId
    data['routerid'] = routerId

    return get_utils(accessToken=accessToken).cloud_http_request.post(
        networkUrls.loadVpcRouterByVpcNetwork.value, data=data)


def unloadVpcRouterByVpcNetwork(accessToken, vpcNetworkId):
    """
    VPC网络卸载路由器
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :return:
    """
    operationType = 8407
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['nid'] = vpcNetworkId

    return get_utils(accessToken=accessToken).cloud_http_request.post(
        networkUrls.unloadVpcRouterByVpcNetwork.value, data=data)


def addVpcNetworkRange(accessToken, vpcNetworkId, **kwargs):
    """
    添加VPC网络IP段
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param kwargs:


    :return:
    """
    operationType = 8404
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['nid'] = vpcNetworkId


def deleteVpcNetworkRange(accessToken, vpcNetworkId, ipRangeIds):
    """
    删除VPC网络IP段
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param ipRangeIds: IP段UUID
    :return:
    """
    operationType = 8405
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['nid'] = vpcNetworkId
    data['ids'] = ipRangeIds

    return get_utils(accessToken=accessToken).cloud_http_request.post(
        networkUrls.deleteVpcNetworkRange.value, data=data)


def getNetworkRangeByVpcNetwork(accessToken, vpcNetworkId, ipType: int = 1, **kwargs):
    """
    获取VPC网络IP段
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param ipType: Ip类型1:IPV4;2:IPV6
    :param kwargs: offset:
                   limit:
    :return:
    """
    return get_utils(accessToken=accessToken).cloud_http_request.get(networkUrls.getNetworkRangeByVpcNetwork.value,
                                                                     {
                                                                         'nid': vpcNetworkId,
                                                                         'type': ipType,
                                                                         'offset': kwargs.get('offset', OFFSET),
                                                                         'limit': kwargs.get('limit', LIMIT)
                                                                     })


def getMountableVMByVpcNetwork(accessToken, vpcNetworkId, **kwargs):
    """
    获取VPC网络下可挂载虚拟机
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param kwargs: offset:
                   limit:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken=accessToken).cloud_http_request.get(networkUrls.getMountableVMByVpcNetwork.value,
                                                                     {
                                                                         'nid': vpcNetworkId,
                                                                         'offset': kwargs.get('offset', OFFSET),
                                                                         'limit': kwargs.get('limit', LIMIT),
                                                                         'keyword': kwargs.get('keyword', ''),
                                                                     })


def getMountedVMByVpcNetwork(accessToken, vpcNetworkId, shared: bool = False, **kwargs):
    """
    获取VPC网络下已挂载虚拟机
    :param accessToken:
    :param vpcNetworkId: VPC网络UUID
    :param shared: 是否是全局共享的，默认False
    :param kwargs: offset:
                   limit:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken=accessToken).cloud_http_request.get(networkUrls.getMountedVMByVpcNetwork.value,
                                                                     {
                                                                         'nid': vpcNetworkId,
                                                                         'shared': 'true' if shared else 'false',
                                                                         'offset': kwargs.get('offset', OFFSET),
                                                                         'limit': kwargs.get('limit', LIMIT),
                                                                         'keyword': kwargs.get('keyword', ''),
                                                                     })
